/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package speech;

import com.ibm.speech.util.Install;
import javax.speech.*;
import javax.speech.synthesis.*;

/**
 *
 * @author Gutenberg
 */
public class Sintetizador {

    static Synthesizer synthesizer;
    static EngineListener engineListener = new EngineAdapter() {

        public void engineError(EngineErrorEvent e) {
            System.out.println(
                    "Engine error: " + e.getEngineError().getMessage());
        }
    };

    public Sintetizador() {
        Install.main(null);
        try {
            synthesizer = Central.createSynthesizer(null);
            if (synthesizer != null) {
                System.out.println("Sintetizador criado.");
                synthesizer.allocate();
                synthesizer.addEngineListener(engineListener);
            }

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public static void speak(String s) {
        String aux = " <JSML> "
			+ "<BREAK MSECS=\"300\"/>"
			+ "<PROS PITCH=\"80\" RANGE=\"50\" RATE=\"150\">"
			+ s
			+ "</PROS>"
			+ " </JSML> ";
        if (synthesizer != null) {
            try {
                synthesizer.speak(aux, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(s);
        }
    }

    public static void main(String args[]) {
        Sintetizador vp = new Sintetizador();
        String texto = " <JSML> "
                + "<BREAK MSECS=\"300\"/>"
                + "<PROS PITCH=\"80\" RANGE=\"50\" RATE=\"150\">"
                + "Testando o Sintetizador."
                + "</PROS>"
                + " </JSML> ";
        Sintetizador.speak(texto);
    }
}
